package com.lyn.plugin.annotation

import com.android.build.api.instrumentation.AsmClassVisitorFactory
import com.android.build.api.instrumentation.ClassContext
import com.android.build.api.instrumentation.ClassData
import com.lyn.plugin.other.AnnotationScanResult
import com.lyn.plugin.other.MyInstrumentationParameters
import org.objectweb.asm.ClassVisitor

/**
 *
 * @author longyn 2025/07/05
 * @version 1.0.0
 */
abstract class AnnotationScanClassVisitorFactory :
    AsmClassVisitorFactory<MyInstrumentationParameters> {

    override fun isInstrumentable(classData: ClassData): Boolean {
        classData.classAnnotations.forEach {
            if (it.endsWith("PluginLauncher")){
                AnnotationScanResult.PLUGIN_LAUNCHER_CLASS_NAME = classData.className
                println("> Task :[AnnotationScanClassVisitorFactory:scan className:${classData.className}]")
                return true
            }
        }
        return false
    }

    override fun createClassVisitor(
        classContext: ClassContext,
        nextClassVisitor: ClassVisitor
    ): ClassVisitor {
        val generatedOutputFile = parameters.get().generatedOutputDir.get().asFile
        val intermediatesOutputFile = parameters.get().intermediatesOutputDir.get().asFile
        return AnnotationClassVisitor(generatedOutputFile,intermediatesOutputFile,nextClassVisitor)
    }
}